package com.hyfd.task;

import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.Base64Utils;
import com.hyfd.common.utils.OPSignatureUtil;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class HaoBaiFlowTask
{
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
    @Autowired
    OrderDao orderDao;// 订单
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    private static Logger log = Logger.getLogger(HaoBaiFlowTask.class);
    
//     @Scheduled(fixedDelay = 60000)
    public void queryBeiMiOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        
        int flag = -1;
        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIkvcjD/7ecMFqF5ulVUV7cSRDlacXGL0793ugQP+PYuI7giHVvQRZXOqRf9KtR46lBDIi3BbcPmiHDyMSddsc+bCCqkmaXq41AVVlB54XwH3WeCeBFm/eRlcKqqvAKBGkOUJ7qfQTArlwAxr9vxF4aWNNzD4TnZgN7Gvu+sRPvnAgMBAAECgYBui/A98g54j/IwnxH7LuMcIDkf6nTeJeo1MTqRiXl6j8Wy3LLByLw6MvVp4lXAL8vJb7+8FVHpefza3x+i9EqSrUHc8jQ12+n9SrYBPTwg5xMCj+QS43LsGF6hoNyUCJyVZl6iBt2Baytdbaa3ypGf/JcOVlbmNKF3cvHfOrHtqQJBAMi/+yfAJyekmXE4Ja3BDOkVg0dxOCW6bHVETTxIxdBXdYMiYA7QzJ/vKy/bEkC/j29pvc1naHM+M3iBrA0Qx50CQQCu8PbB4vZEUcXQrKT3lSBs43wwkBTswOskUGau0Zp2xcCNTOGVV5LAdwyMqEa0Lki+QedhNhTg6uy0rLJybBRTAkA29lD2HibaNLeKBvxR6RP20NwDeF0N/09of83Bno8CLp/Oyl+QTtxnQV0KmikSLyA/Oa0sio5PVw1ShfYsEZcFAkEAlqMG140HYWzIWRdQNaBDe3+mwGpQmkzELVDOGlqCFyPQ2dsJo/70bqYUOEnuF9iC6tD029AlykZ/7FOIlN5b4QJBAKJHdTnpEJ0g74pKNRVixk5a9CkkqSY6e6tB5A1UA7L34fVA5iD2plOz3PY0AqDSRDFUe1ZATgBaX8ZpDbpKZAc=";
        try
        {
            String id = "0000000095";
            Map<String, Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);// 获取通道的数据
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String queryOrderUrl = paramMap.get("queryOrderUrl") + "";// 查询地址
            String terminalName = paramMap.get("terminalName");
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", id);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);
            for (Map<String, Object> order : orderList)
            {
                String orderId = order.get("orderId") + "";
                map.put("orderId", orderId);
                String signature ="";
                Map<String, String> paramMaps = new HashMap<String, String>();
                param.put("terminalName", terminalName);
                param.put("customerOrderId", orderId);
                param.put("orderType", "1");
                try
                {
                    signature = OPSignatureUtil.sign(paramMaps, privateKey);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
//                String signature = sign(custName, orderId, "1", privateKey);
                String url =
                    queryOrderUrl + "?customerOrderId=" + orderId + "&orderType=1&terminalName=" + terminalName + "&signature="
                        + signature;
                String result = ToolHttp.get(false, url);
                if (result != null && !result.equals(""))
                {
                    JSONObject resultJson = JSONObject.parseObject(result);
                    String code = resultJson.getString("code");
                    String message = resultJson.getString("message");
                    String data = resultJson.getString("data");
                    if ("0".equals(code))
                    {
                        
                        JSONObject dataJson = JSONObject.parseObject(data);
                        String status = dataJson.getString("status");
                        if ("0".equals(status))
                        {
                        	map.put("resultCode", status + ":" + message);
                            flag = 1;
                        }
                        else if ("1".equals(code))
                        {
                            continue;
                        }
                        else
                        {
                        	map.put("resultCode", status);
                            flag = 0;
                        }
                    }
                }
                else
                {
                    continue;
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("beimi流量查询Task出错" + e);
        }
    }
    
}
